import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/water_fall/toast.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class waterfall extends StatefulWidget {
  var id;

  waterfall(this.id);

  waterfallWidget createState() => new waterfallWidget(id);
}

class waterfallWidget extends State<waterfall> {
  Widget _body;
  var id ;
  var _scrollController = new ScrollController(initialScrollOffset: 0);
  waterfallWidget(this.id);

  getData() async {
    print(1);
    Dio dio = new Dio();
    Response response = await dio.get(
        "https://api.themoviedb.org/3/movie/$id/credits?api_key=80ac1d0d481f2a1e3e51b5cf5703cf8b");
    var _data = JsonDecoder().convert(response.toString());
    List cast = _data['cast'];
    List crew = _data["crew"];
    print(cast.length);
    cast.addAll(crew);
    print(cast.length);
    setState(() {
      _body = RefreshIndicator(
        child: StaggeredGridView.countBuilder(
          controller:_scrollController ,
          padding: EdgeInsets.all(5.0),
          crossAxisCount: 4,
          itemCount: cast.length,
          itemBuilder: (BuildContext context, int position) {
            return Card(
              child: Column(
                children: <Widget>[
                  SizedBox(
                      width: double.infinity,
                      child:ClipRRect(
                        child: cast[position]["profile_path"] != null
                            ? FadeInImage(
                          placeholder: AssetImage('assets/loading.gif'),
                          image: NetworkImage("https://image.tmdb.org/t/p/w500" +
                              cast[position]["profile_path"]),
                        )
                            : Image(image: AssetImage('assets/na.jpg'),
                        ),
                        borderRadius: BorderRadius.all(
                            Radius.circular(5.0)),
                      )
                  ),
                  Container(
                    padding: EdgeInsets.all(3.0),
                    child: Text(cast[position]['name'],style: TextStyle(fontSize: 15.0),),
                  )
                ],
              ),
            );
          },
          staggeredTileBuilder: (int index) =>
          new StaggeredTile.fit(2),
          mainAxisSpacing: 8.0,
          crossAxisSpacing: 8.0,
        ),
        onRefresh:_onRefresh,
      );
    });
  }
  Future<void> _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 1500));
    setState(() {
      Toast.toast(context,msg: '没有更多数据',position: ToastPosition.bottom);
    });
    print("_onRefresh");
  }
  Future<void> _onLoadMore() async {
    setState(() {
      Toast.toast(context,msg: '加载更多',position: ToastPosition.bottom);
    });
    await await Future.delayed(Duration(milliseconds: 1500));  //模拟网络请求
    setState(() {
      Toast.toast(context,msg: '没有更多数据',position: ToastPosition.bottom);
    });
    print("_onLoadMore");
  }
  void initState() {
    _body = Center(
      child: CircularProgressIndicator(),
    );
    getData();
    _scrollController.addListener(() {
      var px = _scrollController.position.pixels;
      if (px == _scrollController.position.maxScrollExtent) {
        print("加载更多！");
        _onLoadMore();
      }
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text('演员表/工作人员'),
        centerTitle: true,
      ),
      body: _body,
    );
  }
}
